<?php

namespace app\admin\controller\pt;

use app\common\controller\Backend;
use app\common\library\Export as ExportLib;

/**
 * 超计划报警统计
 *
 * @icon fa fa-circle-o
 */
class AlarmMonth extends Backend
{

    /**
     * AlarmMonth模型对象
     * @var \app\admin\model\pt\AlarmMonth
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\pt\AlarmMonth;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $monthId = date('Ym', strtotime($this->request->get('query_date') . "-01"));
            
            $limit = 100;
            $list = $this->model
                ->where($where)
                ->where('month_id', $monthId)
                ->with('depart')
                ->order($sort, $order)
                ->paginate($limit);

            $items = $list->items();
            foreach ($items as &$item) {
                if ($item['month_fee'] > 0) {
                    $item['percent'] = round(($item['consume_fee'] / $item['month_fee']) * 100, 2);
                } else {
                    $item['percent'] = 0;
                }
            }
            unset($item);

            $result = array("total" => $list->total(), "rows" => $items);

            return json($result);
        }
        $this->view->assign('query_date', date('Y-m'));
        return $this->view->fetch();
    }

    public function export()
    {
        $monthId = date('Ym', strtotime($this->request->get('query_date') . "-01"));

        $limit = 100;
        $list = $this->model
            ->where('month_id', $monthId)
            ->with('depart')
            ->order('id', 'asc')
            ->paginate($limit);

        $data = [];
        $items = $list->items();
        foreach ($items as $item) {
            if ($item['month_fee'] > 0) {
                $item['percent'] = round(($item['consume_fee'] / $item['month_fee']) * 100, 2);
            } else {
                $item['percent'] = 0;
            }
            $data[] = [
                $item['depart']['name'],
                $item['month_fee'],
                $item['consume_fee'],
                $item['percent'] . '%',
                $item['plan_total'],
                $item['consume_total'],
                $item['plan_total'] - $item['consume_total'],
            ];
        }

        $savePath = '/uploads/export/'.date('Ymd') ;
        $filename =  $monthId .'(部门超计划报警)_' . uniqid() .'.xlsx';
        $filePath = ROOT_PATH . 'public' . $savePath;
        $fileSaveName =  $filePath  . '/' . $filename;
        if (!is_dir($filePath)) {
            @mkdir($filePath, 0755, true);
        }
        $headers = [
            '部门名称',
            '月计划费用',
            '当前已用费用',
            '占比月计划',
            '当月计划人次',
            '当月已用人次',
            '月剩余人次',
        ];
        $exportLib = new ExportLib($headers, $data, $fileSaveName, 'save');
        $exportLib->createTable();
        $this->success('导出成功', '', ['file' => cdnurl($savePath . '/' .$filename, true)]);
    
    }

}
